package com.oa.works.controller;

import com.alibaba.fastjson.JSONObject;
import com.oa.works.common.util.R;
import com.oa.works.controller.from.TestSayHelloForm;
import com.oa.works.service.ExeclOutputService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/test")
@Api(tags = {"测试Web接口"})
public class TestController {

    @Autowired
    ExeclOutputService execlOutputService;

    @PostMapping("/sayHello")
    @ApiOperation("最简单的测试方法")
    public R sayHello(@Valid @RequestBody TestSayHelloForm form){
        return R.ok().put("message","Hello,"+form.getName());
    }


    @PostMapping("/execlOutputTest1")
    @ApiOperation(value = "数据导出测试1")
    public JSONObject execlOutputTest1(){
        //生成文件名
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmss");
        String filename = "D:\\所有文本资料\\文本资源\\"+df.format(date)+".xlsx";
        JSONObject json = new JSONObject();
        try{
            String code = execlOutputService.execlOutputTest1(filename);
            json.put("code",code);
            json.put("address",filename);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code","500");
            json.put("message","导出错误");
        }
        return json;
    }

    @PostMapping("/addUser")
    @ApiOperation("添加用户")
    @RequiresPermissions(value = {"ROOT", "USER:ADD"}, logical = Logical.OR)
    public R addUser() {
        return R.ok("用户添加成功");
    }


    @PostMapping("IOTest")
    @ApiOperation("IOTest")
    public void IOTest(){}


}

